package com.ikkong.platform.controller;

import java.util.List;
import java.util.Map;

import com.ikkong.core.base.BaseController;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.platform.meta.intercept.MemberIntercept;
import com.ikkong.platform.model.Member;
import com.ikkong.platform.model.Template;
import com.ikkong.platform.service.MemberService;
import com.ikkong.platform.service.impl.MemberServiceImpl;

/**
 * Generated by Blade. 2018-06-27 10:32:32
 */
public class MemberController extends UrlPermissController {
	private static String CODE = "member";
	private static String PERFIX = "tb_pt_member";
	private static String LIST_SOURCE = "Member.list";
	private static String BASE_PATH = "/platform/member/";

	MemberService service = new MemberServiceImpl();

	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "member.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "member_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Member member = service.findById(id);
		setAttr("model", JsonKit.toJson(member));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "member_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Member member = service.findById(id);
		setAttr("model", JsonKit.toJson(member));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "member_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE, new MemberIntercept());
		renderJson(grid);
	}

	public void save() {
		Member member = mapping(PERFIX, Member.class);
		boolean temp = service.save(member);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Member member = mapping(PERFIX, Member.class);
		member.setVersion(member.getVersion() + 1);
		boolean temp = service.update(member);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}

	/**
	 * 获取使用客户
	 * */
	@ClearShiro
	public void getMemberList() {
		// String id = getPara(0);

		List<Member> memberList = null;
		memberList = service.findBy("id,name", "1=1", null);
		if (memberList != null) {
			renderJson(memberList);
		}
		/*
		 * if (id.isEmpty()) { renderJson(error(SAVE_FAIL_MSG)); } else {
		 * templateList = service.findBy("id,name",
		 * "bussiness_id=#{bussiness_id}", Record.create().set("bussiness_id",
		 * id)); renderJson(templateList); }
		 */
	}
}
